package com.bdqn.t350.vue.controller;


import com.bdqn.t350.vue.bean.Dto;
import com.bdqn.t350.vue.bean.User;
import com.bdqn.t350.vue.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api(value = "用户登录退出", tags = "用户登录退出tags")
@Controller
public class LoginController {

    @Resource
    UserService userService;

    @Resource
    StringRedisTemplate stringRedisTemplate;


    @CrossOrigin
    @GetMapping("/")
    @ResponseBody
    public Dto index() {
        System.out.println("index");
        return new Dto();
    }


    /**
     * 登录
     *
     * @param loginUser
     * @param response
     * @return
     */
    @CrossOrigin
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "查询用户列表", httpMethod = "POST", response = Dto.class)
    public Dto login(@RequestBody User user, HttpServletResponse response) {
//        User sysUser = new User();
//        Dto dto = new Dto();
//        if (loginUser.getUsrName() != null && loginUser.getUsrPassword() != null) {
//            sysUser = userService.selectByName(loginUser.getUsrName());
//            if (sysUser == null || !sysUser.getUsrPassword().equals(loginUser.getUsrPassword())) {
//                dto.setSuccess("false");
//            } else {
//                //产生一个比较随意的token
//                String token = UUID.randomUUID().toString();
//                System.out.println("token:" + token);
//                //在执行完登录操作后,会讲token放到response响应头中将token返回给客户端
//                //response.setHeader("token", uuid);
//                dto.setToken(token);
//                dto.setSuccess("true");
//                stringRedisTemplate.opsForValue().set(token, sysUser.getUsrName(), 600, TimeUnit.SECONDS);
//            }
//        } else {
//            dto.setSuccess("false");
//        }
//        return dto;
        return userService.login(user);
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/logout")
    @CrossOrigin
    @ResponseBody
    public Dto logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        Dto dto = new Dto();
        if (stringRedisTemplate.delete(token) == true)
            dto.setSuccess("true");
        else
            dto.setSuccess("false");
        return dto;
    }

    /**
     * 注意这里有点坑,logout路径会跳转到security自带的注销接口,
     * 导致自己定义的这个注销接口功能无法正确实现。
     * 所以必须更改自己定义的注销接口路径（不能是/logout）
     *
     * @return
     */
    @RequestMapping(value = "/userlogout")
    @CrossOrigin
    @ResponseBody
    public Dto userlogout() {
        return userService.logout();
    }
}
